package com.hcrazy.manchester.app.jdbc;

import java.util.List;

import com.hcrazy.manchester.app.pojo.NewsSort;

public class NewsSortDAO extends BaseDAO<NewsSort>{

	public void addNewsSort(NewsSort sort) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into him_news_sort(");
		sb.append("sortName,sortOrder,parentId,moduleType");
		sb.append(") values (");
		sb.append(":sortName,:sortOrder,:parentId,:moduleType");
		sb.append(")");
		
		update(sb.toString(), sort);
	}
	
	public void updateNewsSort(NewsSort sort) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("update him_news_sort set ");
		sb.append(" sortName=:sortName,sortOrder=:sortOrder,parentId=:parentId,moduleType=:moduleType");
		sb.append(" where sortId = :sortId");
		
		update(sb.toString(), sort);
	}
	
	public List<NewsSort> listAllSorts(int type) throws Exception{
		String sql = "select * from him_news_sort where moduleType = ? order by sortOrder";
		return getList(sql, NewsSort.class,type);
	}
	
	public NewsSort getById(int sortId) throws Exception{
		String sql = "select * from him_news_sort where sortId = ?";
		return this.getJavaBean(sql, NewsSort.class, sortId);
	}
	
	public List<NewsSort> getByParentId(int parentId) throws Exception{
		String sql = "select * from him_news_sort where parentId = ?";
		return getList(sql, NewsSort.class,parentId);
	}
	
	public void delNewsSort(int sortId) throws Exception{
		String sql = "delete from him_news_sort where sortId = ?";
		this.commonUpdate(sql, sortId);
	}
	
	public void delNewsSortByParentId(int parentId) throws Exception{
		String sql = "delete from him_news_sort where parentId = ?";
		this.commonUpdate(sql, parentId);
	}
	
	public void delNewsSortWithoutParent() throws Exception{
		
	}
	
	public boolean hasChildren(int parentId) throws Exception{
		String sql = "select count(*) from him_news_sort where parentId = ?";
		int count = getCount(sql, parentId);

		return count > 0;
	}
}
